//
//  MY_ViewController.m
//  YUXiu
//
//  Created by 王竞成 on 16/6/22.
//  Copyright © 2016年 Wang. All rights reserved.
//

#import "MY_ViewController.h"
#import "MY_infoModel.h"
#import "MytableHraderView.h"
#import "GiftListViewController.h"
#import "TableHeaderViewCell.h"
#import "MY_CollectionViewCell.h"
#import "MY_Layout.h"
#import "SetViewController.h"

@interface MY_ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    MY_infoModel *InfoModel;
    
    UICollectionView *collectView;
    NSArray *cellectionfalseData;

    BOOL isSign;
}

@end

@implementation MY_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
#pragma mark - UI操作
-(void)autoConfigViews{
    cellectionfalseData = @[@"我的课程",@"我的直播",@"我的订单",@"充值",@"收看历史",@"消息提醒",@"拼团"];
    [self.tableView setBackgroundColor:kNaviBarCOLOR];
    
    self.navigationController.navigationBar.translucent = NO;
    [self initNav];
    UIView *view_bg = [[UIView alloc] initWithFrame:krect(0, -64, kScreenWidth, 64)];
    [self.view addSubview:view_bg];

    self.tableView.tableHeaderView = [self getHeaderView];
    
    [self.view bringSubviewToFront:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"TableHeaderViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
}
-(UIView *)getHeaderView{
    UIView *bgView = [[UIView alloc]initWithFrame:krect(0, 0, kScreenWidth, 214 + 64 +64)];
    bgView.backgroundColor = kOrangeCOLOR;
    UIView * view= [[MytableHraderView alloc]initWithFrame:krect(30, 64, kScreenWidth-60, 214+44) model:InfoModel];
    
    [bgView addSubview:view];
    return bgView;
}

//导航栏
- (void)initNav{
    //礼物榜
    UIButton *giftBtn = [[UIButton alloc]initWithFrame:krect(10, 27, 30, 30)];
    [giftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    giftBtn.tag = 1000;
    [giftBtn setImage:kCreatImage(@"gift") forState:0];
    [self.navigationController.view addSubview:giftBtn];

    //签到
    UIButton *signBtn = [[UIButton alloc]initWithFrame:krect(giftBtn.frame.origin.x + giftBtn.golf_width + 13, 27, 50, 30)];
    [signBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [signBtn setBackgroundColor:kWhiteCOLOR];
    
    [signBtn.layer setMasksToBounds:YES];
    [signBtn.layer setCornerRadius:5];
    [signBtn.layer setBorderWidth:1];
    [signBtn.layer setBorderColor:[[HexRGB(0xFFFFFF) colorWithAlphaComponent:0.25] CGColor]];

    [signBtn setTitle:@"签到" forState:UIControlStateNormal];
    signBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    [signBtn setTitleColor:kOrangeCOLOR forState:UIControlStateNormal];
    
    signBtn.tag = 1001;
    [self.navigationController.view addSubview:signBtn];
    
    //设置
    UIButton *settingBtn = [[UIButton alloc]initWithFrame:krect(kScreenWidth - 10 -30, 27, 30, 30)];
    [settingBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    settingBtn.tag = 1002;
    [settingBtn setImage:[UIImage imageNamed:@"Personalcenter_nav_icn_seting_normal"] forState:0];
    [self.navigationController.view addSubview:settingBtn];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"";
    self.navigationItem.backBarButtonItem = backItem;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    for (int i = 0; i < 3; i ++) {
        UIButton *btn = (UIButton *)[self.navigationController.view viewWithTag:1000 + i];
        btn.hidden = YES;
    }

    self.navigationController.navigationBar.barTintColor = kNaviBarCOLOR;//导航栏颜色
    [self.navigationController.navigationBar setTranslucent:NO];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(void)viewWillAppear:(BOOL)animated
{

    self.navigationItem.title = @"";
    [super viewWillAppear:YES];
    for (int i = 0; i < 3; i ++) {
        UIButton *btn = (UIButton *)[self.navigationController.view viewWithTag:1000 + i];
        btn.hidden = NO;
    }
    [self.navigationController.navigationBar setTranslucent:YES];
    if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)])
    {
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }
    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bigShadow"] forBarMetrics:UIBarMetricsCompact];

}
#pragma mark - tableView
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0) return 60;
    else return 280;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        TableHeaderViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        return cell;
    }else{
        static NSString *identy = @"idcell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identy];
        }
        MY_Layout *layout = [[MY_Layout alloc]init];
        collectView = [[UICollectionView alloc]initWithFrame:krect(0, 0, kScreenWidth, 200) collectionViewLayout:layout];
        collectView.delegate = self;
        collectView.dataSource = self;
        collectView.scrollEnabled = NO;
        collectView.backgroundColor = kNaviBarCOLOR;
        
        [cell.contentView addSubview:collectView];
        
        [collectView registerClass:[MY_CollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
        
        return cell;

    }
    
}
#pragma mark - collectionView delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return cellectionfalseData.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MY_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    [cell.iconView setImage:kCreatImage(@"我的订单_")];
    [cell.title setText:cellectionfalseData[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self showHUDWithText:[NSString stringWithFormat:@"点击的是第%ld个item",(long)indexPath.row] AfterDelay:1];
}

#pragma mark - button Click
-(void)btnClick:(UIButton *)sender{
    switch (sender.tag) {
        case 1000:{
            //礼物榜
            GiftListViewController *giftVC = [[GiftListViewController alloc]init];
            [self.navigationController pushViewController:giftVC animated:YES];
        }
            break;
        case 1001:{
            //签到
            UIButton *btn = (UIButton *)[self.navigationController.view viewWithTag:1001];
            if (!isSign) {
                UILabel *signLabel = [[UILabel alloc]initWithFrame:krect(btn.golf_right +3, btn.golf_top+10, 40, 20)];
                signLabel.text = @"+1";
                signLabel.textColor = kWhiteCOLOR;
                signLabel.font = [UIFont boldSystemFontOfSize:16];
                [self.navigationController.view addSubview:signLabel];
                [UIView animateWithDuration:1.5 animations:^{
                    [signLabel setFrame:krect(btn.golf_right +3, -40, 40, 20)];
                } completion:^(BOOL finished) {
                    [signLabel removeFromSuperview];
                    [btn setTitle:@"已签到" forState:UIControlStateNormal];
                    isSign = YES;

                }];
                
            }else{
                [self showHUDWithInfo:@"已签到" AfterDelay:1];
            }
        }
            break;
        case 1002:{
            SetViewController * setVC = [[SetViewController alloc]init];
            [self.navigationController pushViewController:setVC animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
